import pyglet

#Inherit from 'object' in order to become a new-style class: http://docs.python.org/reference/datamodel.html#newstyle
#Otherwise I cannot use the super() method
class GameSprite(object):
    """Base class for the game sprites."""
    LEFT_MOVE  = 0
    RIGHT_MOVE = 1
    SCREEN_WIDTH = 800
    SCREEN_HEIGHT = 600

    def __init__(self, filename, rows, columns, framescount, x, y, dx, dy):
        #Initial position in the X-axis
        self.x = x
        #Initial position in the Y-axis
        self.y = y
        #How many frames from the whole sprite compose the animation?
        self.framescount = framescount
        unaimg = pyglet.resource.image(filename)
        self.sprite = pyglet.sprite.Sprite(unaimg, x = self.x, y = self.y)
        self.direction = self.RIGHT_MOVE
        #Create an ImageGrid with the sprite
        self.grid = pyglet.image.ImageGrid(unaimg, rows, columns)
        #Current index into the ImageGrid
        self.grid_index = 0
        #number of pixels to move in the X-axis on each step
        self.dx = dx
        #number of pixels to move in the Y-axis on each step
        self.dy = dy
        #Width of a sprite frame
        self.spriteWidth = self.grid[0].width
        #Height of a sprite frame
        self.spriteHeight = self.grid[0].height
        
    def getX(self):
        return self.x

    def getY(self):
        return self.y

    def getSprite(self):
        return self.sprite
        
    def getDirection(self):
        return self.direction

    def setX(self, newx):
        #Avoid going beyond the limits of the screen to the left in the X-axis
        if newx < 0:
            self.x = 0
        #Avoid going beyond the limits of the screen to the right in the X-axis
        elif newx > (self.SCREEN_WIDTH - self.spriteWidth):
            self.x = self.SCREEN_WIDTH - self.spriteWidth
        else:
            self.x = newx
    
    def setY(self, newy):
        #Avoid going beyond the limits of the screen downwards in the Y-axis
        if newy < 0:
            self.y = 0
        #Avoid going beyond the limits of the screen upwards in the Y-axis
        elif newy > (self.SCREEN_HEIGHT - self.spriteHeight):
            self.y = self.SCREEN_HEIGHT - self.spriteHeight
        else:
            self.y = newy

    def setPosition(self, newx, newy):
        self.x = newx
        self.y = newy

    def moveX(self, dx):
        self.setX(self.x + dx)
        #Update the current index into the ImageGrid.
        #self.grid_index must be between 0 and self.framescount - 1, so I use modulus self.framescount
        self.grid_index = (self.grid_index + 1) % self.framescount

    def moveY(self, dy):
        self.setY(self.y + dy)
    
    def setDirection(self, new_dir):
        self.direction = new_dir
        
    def getSpriteFrame(self):
        raise Exception('Method getSpriteframe not implemented')
        
    def getSpriteWidth(self):
        return self.spriteWidth
        
    def getSpriteHeight(self):
        return self.spriteHeight

    def moveLeft(self):
        self.moveX(-self.dx)
    
    def moveRight(self):
        self.moveX(self.dx)

    def moveUp(self):
        self.moveY(self.dy)

    def moveDown(self):
        self.moveY(-self.dy)

##########################################################################################
class Janitor(GameSprite):

    def __init__(self, x, y):
        #Call the parent class constructor
        #Parameters: filename, rows, columns, framescount, x, y, dx, dy
        super(Janitor, self).__init__('The_Urchin-breezefarmerat3-alpha.png', 4, 4, 4, x, y, 10, 0)

    def getSpriteFrame(self):
        if self.direction == self.RIGHT_MOVE:
            #self.grid_index + 4: 4 is the first img moving to the right into the ImageGrid
            return self.grid[self.grid_index + 4]
        else:
            #self.grid_index + 8: 8 is the first img moving to the left into the ImageGrid
            return self.grid[self.grid_index + 8]

###################################################################################################
class Bird(GameSprite):

    def __init__(self,x, y):
        #Call the parent class constructor
        #Parameters: filename, rows, columns, framescount, x, y, dx, dy
        super(Bird, self).__init__('bat.png', 8, 12, 3, x, y, 70, 0)

    def moveX(self, dx):
        """Override the parent class implementation."""
        #Update the 'x' value
        self.x += dx
        #Update the current index into the ImageGrid.
        #self.grid_index must be between 0 and 3, so I use modulus 4
        if self.x < 0:
            self.x = 0
            self.direction = self.RIGHT_MOVE
        elif self.x > (800 - 24):
            self.x = 800 - 24
            self.direction = self.LEFT_MOVE
        self.grid_index = (self.grid_index + 1) % 3

    def getSpriteFrame(self):
        if self.direction == self.RIGHT_MOVE:
            #self.grid_index + 4: 4 is the first img moving to the right into the ImageGrid
            return self.grid[self.grid_index + 78]
        else:
            #self.grid_index + 8: 8 is the first img moving to the left into the ImageGrid
            return self.grid[self.grid_index + 54]
